#!/bin/bash
# $Id$
#
SCRIPT_LANG="unknown"
SCRIPT_LANG="python"
SCRIPT_LANG="ruby"
#
DEBUG=0
#
# setup required libs: JAVA_LIBS="../lib/log4j.jar:../lib/junit.jar:../lib/jas.jar"
# check fpr jython or jruby
#
if [ -f "../lib/log4j.jar" ] ; then
   JAVA_LIBS="../lib/log4j.jar"
elif [ -f "lib/log4j.jar" ] ; then
   JAVA_LIBS="lib/log4j.jar"
else
   echo "===========================================" 
   echo "Error: library log4j.jar not found" 
   echo "Please install log4j.jar in lib/ or ../lib/" 
   echo "===========================================" 
   exit 0
fi
#
if [ -f "../lib/junit.jar" ] ; then
   JAVA_LIBS="$JAVA_LIBS:../lib/junit.jar"
elif [ -f "lib/junit.jar" ] ; then
   JAVA_LIBS="$JAVA_LIBS:lib/junit.jar"
else
   echo "===========================================" 
   echo "Warning: library junit.jar not found" 
   echo "Please install junit.jar in lib/ or ../lib/" 
   echo "===========================================" 
fi
#
if [ -d "examples" ] ; then
   JAVA_LIBS="$JAVA_LIBS:examples"
fi
if [ -d "edu/jas" ] ; then
   JAVA_LIBS="$JAVA_LIBS:."
elif [ -f "../lib/jas.jar" ] ; then
   JAVA_LIBS="$JAVA_LIBS:../lib/jas.jar"
elif [ -f "lib/jas.jar" ] ; then
   JAVA_LIBS="$JAVA_LIBS:lib/jas.jar"
elif [ -f "jas.jar" ] ; then
   JAVA_LIBS="$JAVA_LIBS:jas.jar"
else
   echo "===========================================" 
   echo "Error: library jas.jar not found" 
   echo "Please install jas.jar in lib/ or ../lib/" 
   echo "===========================================" 
   exit 0
fi
#
#JAVA_LIBS="$JAVA_LIBS:."
#
#JAVA_OPTS="-J-verbose:gc -J-Xms1100M -J-Xmx1900M"
JAVA_OPTS="-J-verbose:gc -J-Xms350M -J-Xmx800M"
#JAVA_OPTS="-J-verbose:gc"
SCRIPT_OPTS=""
#
# check for scripting language
# Scanning for args is aborted by '--'.
#set -- $JAS_OPTS "$@"
#echo "jas_args: " $*
while [ $# -gt 0 ]
do
    case "$1" in
    # use python
    -py*|-jy*) 
         SCRIPT_LANG="python"
         ;;
    # use ruby
    -rb*|-ruby|-jr*) 
         SCRIPT_LANG="ruby"
         ;;
    -h|--help)
         help_requested=true
         ;;
    -d)
         DEBUG=1
         ;;
    -v|--verbose)
         SCRIPT_OPTS="-v"
         ;;
    --) break ;;
    # Other opts go through  
    -*) jas_args=("${jas_args[@]}" "$1") ;;
    # Abort processing on first non-opt arg 
    *) jas_args=("${jas_args[@]}" "$1")
        break ;;
    esac
    shift
done

if [ "$SCRIPT_LANG" = "python" ]; then
   if [ -f "examples/jas.py" ] ; then
      echo -n "" 
   else 
      echo "===========================================" 
      echo "Error: library examples/jas.py not found" 
      echo "Please install examples/jas.py" 
      echo "===========================================" 
      exit 0
   fi
   TMPX=`which jython`
   if [[ -n $TMPX && -x $TMPX ]] ; then
      echo -n ""
   else 
      echo "===========================================" 
      echo "Error: jython script not found" 
      echo "Please install jython in your PATH" 
      echo "===========================================" 
      exit 0
   fi
fi

if [ "$SCRIPT_LANG" = "ruby" ]; then
   if [ -f "examples/jas.rb" ] ; then
      echo -n "" 
   else 
      echo "===========================================" 
      echo "Error: library examples/jas.rb not found" 
      echo "Please install examples/jas.rb" 
      echo "===========================================" 
      exit 0
   fi
   TMPX=`which jruby`
   if [[ -n $TMPX && -x $TMPX ]] ; then
      echo -n "" 
   else 
      echo "===========================================" 
      echo "Error: jruby script not found" 
      echo "Please install jruby in your PATH" 
      echo "===========================================" 
      exit 0
   fi
fi


#echo "jas_args: " $jas_args
# Put the jas_args back into the position arguments $1, $2 etc
set -- "${jas_args[@]}"

if [ -n "$help_requested" ] ; then
  echo "JAS options:" >&2
  echo "-py|-jy   : use Python scripting with jython" >&2
  echo "-rb|-jr   : use Ruby scripting with jruby" >&2
  echo "-d        : debug this script" >&2
  echo "-v        : verbose option for scripting language" >&2
  echo "-h|--help : this infos" >&2
  echo "--        : pass remaining arguments through to Jython|JRuby" >&2
  exit 0
fi

#
if [ "$SCRIPT_LANG" = "python" ]; then
   [[ $DEBUG > 0 ]] && echo jython $SCRIPT_OPTS -J-cp "$JAVA_LIBS" $JAVA_OPTS $* 
   jython $SCRIPT_OPTS -J-cp "$JAVA_LIBS" $JAVA_OPTS $* 
elif [ "$SCRIPT_LANG" = "ruby" ]; then
   [[ $DEBUG > 0 ]] && echo jruby $SCRIPT_OPTS -J-cp "$JAVA_LIBS" $JAVA_OPTS -S jirb --noinspect --readline -r "examples/jas.rb" $*
   jruby $SCRIPT_OPTS -J-cp "$JAVA_LIBS" $JAVA_OPTS -S jirb --noinspect --readline -r "examples/jas.rb" $*
else 
   echo "===========================================" 
   echo "Unknown scripting language $SCRIPT_LANG" 
   echo "===========================================" 
   exit 0
fi
#
